package santeclair.connexion.service.trame.impl;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import santeclair.connexion.bean.trame.CodeStructureTrameEnum;
import santeclair.connexion.bean.trame.DemandeLectureTrame;
import santeclair.connexion.bean.trame.DonneesTrame;
import santeclair.connexion.dao.parametrage.StructureTrameDao;
import santeclair.connexion.domaine.parametrage.StructureTrame;
import santeclair.connexion.service.trame.InterpreteurTrameService;
import santeclair.connexion.service.trame.LectureTrameService;

/**
 * 
 * @author cquintin
 * 
 */
@Service
public class LectureTrameImpl implements LectureTrameService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LectureTrameImpl.class);

    @Autowired
    private InterpreteurTrameService interpreteurTrameService;

    @Autowired
    private StructureTrameDao structureTrameDao;

    /**
     * {@inheritDoc}
     */
    public DonneesTrame lectureTrame(DemandeLectureTrame demandeLectureTrame, CodeStructureTrameEnum codeStructureTrameEnum, boolean valoriserChampAnalyseTrame) {
        StructureTrame structureTrame = structureTrameDao.findByUniqueParameter("code", codeStructureTrameEnum.name());
        DonneesTrame donneesTrame = codeStructureTrameEnum.getDonneesTrameInstance();
        try {
            interpreteurTrameService.lireTrame(demandeLectureTrame.getTrame(), structureTrame, structureTrame.getBufferedReader(), donneesTrame,
                            valoriserChampAnalyseTrame);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return donneesTrame;
    }

}
